package com.hnkywl.sts.service.base;

import com.hnkywl.sts.dao.base.DxpzDao;
import com.hnkywl.sts.entity.base.Dxpz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import com.hnkywl.sts.util.SMSUtils;
import com.hnkywl.sts.util.StringFormat;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-20
 * Time: 下午8:49
 * To change this template use File | Settings | File Templates.
 */
@Service
public class DxpzService {
    private DxpzDao dxpzDao;
    private DxrzService dxrzService;

    /**
     * 短信配置查询分页
     *
     * @param mc
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findDxpzList(String mc, int pageNo, int pageSize) {
        Page<Dxpz> dxpzPage = this.dxpzDao.findDxpzList(mc, pageNo, pageSize);
        List<Dxpz> dxpzList = dxpzPage.getResult() == null ? new ArrayList<Dxpz>() : dxpzPage.getResult();
        Map<String, Object> otherParam = new HashMap<String, Object>();
        otherParam.put("list_fsrList", "xm");
        String dxpzJson = BaseUtils.fetchCollectionToJson(dxpzList, otherParam, "id", "dxbm", "mc", "fsrList", "sfqy", "bz", "dxmb");
        return "{\"total\":" + dxpzPage.getTotalCount() + ",\"rows\":[" + dxpzJson + "]}";
    }

    /**
     * 保存短信配置信息
     *
     * @param dxpz 短信配置
     * @return
     */
    @Transactional
    public void saveDxpz(Dxpz dxpz) throws RuntimeException {
        dxpz.setDateCreated(new Date());
        dxpz.setLastUpdated(dxpz.getDateCreated());
        this.dxpzDao.save(dxpz);
    }

    /**
     * 更新短信配置信息
     *
     * @param dxpz 短信配置
     * @return
     */
    @Transactional
    public void updateDxpz(Dxpz dxpz) throws RuntimeException {
        Dxpz dx = this.dxpzDao.get(dxpz.getId());
        dx = (Dxpz) ReflectionUtils.getInstance(dx, dxpz);
        dx.setLastUpdated(new Date());
        this.dxpzDao.save(dx);
    }

    /**
     * 删除短信配置信息
     *
     * @param id 短信配置id
     * @return
     */
    @Transactional
    public void deleteDxpz(Long id) {
        Dxpz dxpz = dxpzDao.get(id);
        dxpz.setLastUpdated(new Date());
        dxpz.setDeleted(1);
        this.dxpzDao.save(dxpz);
    }

    //启用短信配置
    @Transactional
    public void startDxpz(Long id) {
        Dxpz dxpz = dxpzDao.get(id);
        dxpz.setLastUpdated(new Date());
        dxpz.setSfqy(true);
        this.dxpzDao.save(dxpz);
    }

    //停用短信配置
    @Transactional
    public void stopDxpz(Long id) {
        Dxpz dxpz = dxpzDao.get(id);
        dxpz.setLastUpdated(new Date());
        dxpz.setSfqy(false);
        this.dxpzDao.save(dxpz);
    }

    /**
     * 查询短信配置明细
     *
     * @param id 短信配置id
     * @return
     */
    @Transactional
    public Dxpz findDxpzDetail(Long id) {
        return this.dxpzDao.get(id);
    }

    /**
     * 采购结算发送短信
     *
     * @param params
     * @throws Exception
     */
    public void sendMsgForCgjs(String... params) {
        sendMsg("cgjs", params);
    }

    /**
     * 物流结算发送短信
     *
     * @param params
     */
    public void sendMsgForWljs(String... params) {
        sendMsg("wljs", params);
    }

    /**
     * 流程发送短信
     *
     * @param ygs
     * @param params
     */
    public void sendMsgForFlow(List<Yg> ygs, String... params) {
        sendMsg("flow", ygs, params);
    }

    /**
     * 测试发送短信
     *
     * @param ygs
     * @param params
     */
    public void sendMsgForTest(List<Yg> ygs, String... params) throws Exception {
        sendTestMsg("test", ygs, params);
    }

    public void sendMsg(String pzbm, String... params) {
        sendMsg(pzbm, null, params);
    }

    /**
     * 发送短信测试返回异常
     *
     * @param pzbm
     * @param ygs
     * @param params
     * @throws Exception
     */
    public void sendTestMsg(String pzbm, List<Yg> ygs, String... params) throws Exception {
        sendMsgEx(pzbm, ygs, params);
    }

    public void sendMsg(String pzbm, List<Yg> ygs, String... params) {
        try {
            sendMsgEx(pzbm, ygs, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //现金交易短信提醒
    public void sendSMS(String pzbm, String lxhm, String... params) {
        Dxpz dxpz = this.dxpzDao.findByBm(pzbm);
        if (dxpz != null && StringUtils.isNotEmpty(lxhm)) {
            try {
                SMSUtils.getInstance().send(lxhm, StringFormat.format(dxpz.getDxmb(), params));
                dxrzService.saveDxrz(lxhm, StringFormat.format(dxpz.getDxmb(), params), true, "发送成功");
            } catch (Exception e) {
//                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                dxrzService.saveDxrz(lxhm, StringFormat.format(dxpz.getDxmb(), params), false, e.getMessage());
            }
        }
    }

    public void sendMsgEx(String pzbm, List<Yg> ygs, String... params) {
        Dxpz dxpz = this.dxpzDao.findByBm(pzbm);
        List<Yg> fsrs = ygs;

        if (fsrs == null) {
            fsrs = dxpz.getFsrList();
        }

        if (dxpz != null && fsrs.size() > 0) {

            try {
                SMSUtils.getInstance().send(getFsrPhoneNO(fsrs), StringFormat.format(dxpz.getDxmb(), params));
                dxrzService.saveDxrz(fsrs, StringFormat.format(dxpz.getDxmb(), params), true, "发送成功");
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                dxrzService.saveDxrz(fsrs, StringFormat.format(dxpz.getDxmb(), params), false, e.getMessage());
            }

        }
    }

    public List<String> getFsrPhoneNO(List<Yg> ygs) {
        List<String> result = new ArrayList<String>();
        for (Yg yg : ygs) {
            if (yg.getDhhm() == null && "".equals(yg.getDhhm())) continue;
            result.add(yg.getDhhm());
        }

        return result;
    }

    @Autowired
    public void setDxpzDao(DxpzDao dxpzDao) {
        this.dxpzDao = dxpzDao;
    }

    @Autowired
    public void setDxrzService(DxrzService dxrzService) {
        this.dxrzService = dxrzService;
    }
}
